﻿'// ========================================================================= '
'// ------------------------------------------------------------------------- '
'//!				\file		CAa_Lst.vb
'			        \date		june 2010
'			        \author		TNick
'
'			        \brief		
'
'
'   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'   This file is considered universal knowledge. No restrictions
'   are imposed; use as you see fit.
'   Please read ReadMe.txt and Licence.txt in root folder
'   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'
'   Shows a list where the user may input a list of areas
'
'// ------------------------------------------------------------------------- '
'// ========================================================================= '
'//
'//
'//
'//
'//  IMPORTS     ------------------------------------------------------------ '
#Region "Imports"

Imports System.Windows.Forms

#End Region
'//  IMPORTS     ============================================================ '
'//
'//
'//
'//
'//  DEFINITIONS    --------------------------------------------------------- '

'//  DEFINITIONS    ========================================================= '
'//
'//
'//
'//
'//  DATA    ---------------------------------------------------------------- '


'//  DATA    ================================================================ '
'//
'//
'//
'//
'//  CLASS    --------------------------------------------------------------- '

Public Class CAa_Lst

    '// --------------------------------------------------------------------- '
    '// DATA
    Dim dMaxA As Double
   '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    '// API's

    '// ===================================================================== '

    Public Sub New(ByVal dMaxArea As Double)
        InitializeComponent()
        dMaxA = dMaxArea
    End Sub


    ' /////////////////////////////////////////////////////////////////////////
    #Region "Private functions"


    '// --------------------------------------------------------------------- '
    '// returns -1 for errors that were shown to the user
    '// 0 for errors that were not shown
    '// > 0 number of valid entries
    Private Function ParseUIText() as Integer
        Dim parts() As String = Split(Me.txIn.Text, vbCrLf)

        Dim i As Integer
        Dim dTotArea As Double
        Dim iErr As Integer = 0
        Dim iOk As Integer = 0
        Dim InLSep(0 To 4) As Char
        InLSep(0) = " "c
        InLSep(1) = CChar(vbTab)
        InLSep(2) = "|"c
        InLSep(3) = "~"c
        InLSep(4) = "^"c

        LW_Out.Items.Clear()

        For i = 0 to UBound(parts)
            parts(i) = parts(i).Trim
            If (parts(i).Length = 0) Then
                LW_Out.Items.Add("(" & Cmain._T(TX.SKIP) & ")")
                Continue For
            End If
            Dim iSp As Integer = parts(i).IndexOfAny(InLSep)
            Dim dArea As Double
            If (iSp < 0) Then
                ' // only the area
                Try
                    dArea = CDbl(parts(i))
                    dTotArea = dTotArea + dArea
                    LW_Out.Items.Add(CStr(dArea))
                    iOk = iOk + 1
                Catch ex As System.Exception
                    LW_Out.Items.Add("(" & _ 
                                     Cmain._T(TX.ERRORIN) & "<" & _
                                     parts(i) & ">)")
                    iErr = iErr + 1
                End Try
            Else
                Try
                    dArea = CDbl(parts(i).Substring(0,iSp))
                    LW_Out.Items.Add(CStr(dArea))
                    iOk = iOk + 1
                    dTotArea = dTotArea + dArea
                Catch ex As System.Exception
                    LW_Out.Items.Add("(" & _ 
                                     Cmain._T(TX.ERRORIN) & "<" & _
                                     parts(i).Substring(0,iSp) & ">)")
                    iErr = iErr + 1
                End Try
                LW_Out.Items(i).SubItems.Add(parts(i).Substring(iSp+1))
            End If
        Next i
        If (iErr > 0) Then Return 0
        If (iOk = 0) Then Return 0
        If (dTotArea > dMaxA) Then
            MsgBox(String.Format( _
                            Cmain._T(TX.ERR_PD_AHP), _
                            Cstr(dTotArea) & " > " & Cstr(dMaxA)))
            Return -1
        End If
        Return iOk
    End Function
    '// ===================================================================== '

    #End Region
    ' /////////////////////////////////////////////////////////////////////////


    ' /////////////////////////////////////////////////////////////////////////
    #Region "Event handlers"


    '// --------------------------------------------------------------------- '
    Private Sub OK_Button_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles OK_Button.Click

        Dim i As Integer = ParseUIText()
        If (i = 0) Then
            Call MsgBox(CMain._T(TX.ERR_PALST))
        Else If (i > 0) Then
            CM_PolyArea.Stgs.bAskDir = Me.ckAskDir.Checked
            ReDim CM_PolyArea.lArea(0 to i-1)
            Dim j As Integer
            i = 0
            For j = 0 to LW_Out.Items.Count - 1
                If (LW_Out.Items(j).Text.StartsWith("(") = False) Then
                    CM_PolyArea.lArea(i).dArea = CDbl(LW_Out.Items(j).Text)
                    If (LW_Out.Items(j).SubItems.Count > 1) Then
                    If (LW_Out.Items(j).SubItems(1).Text.Length > 0) Then
                        CM_PolyArea.lArea(i).sName = _
                            LW_Out.Items(j).SubItems(1).Text
                    Else
                        CM_PolyArea.lArea(i).sName = Nothing
                    End If
                    End If
                   i = i + 1
                End if
            Next
            CM_PolyArea.Stgs.sTxLstA = Me.txIn.Text 
            Me.DialogResult = System.Windows.Forms.DialogResult.OK
            Me.Close()
        End If
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub Cancel_Button_Click( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles Cancel_Button.Click

        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub CA_List_Load( _
        ByVal sender As System.Object, _
        ByVal e As System.EventArgs) _
        Handles MyBase.Load


        Me.Text = CMain._T(TX.PA_CAPLIST)
        Me.Cancel_Button.Text = CMain._T(TX.CANCEL)
        Me.OK_Button.Text = CMain._T(TX.OK)

        Me.TP_In.Text = CMain._T(TX.INPUT)
        Me.TP_In.Text = CMain._T(TX.OUTPUT)
        
        Me.ckAskDir.Text = CMain._T(TX.PA_ASKDIR)
        Me.ckAskDir.Checked = CM_PolyArea.Stgs.bAskDir


        LW_Out.View = View.Details
        LW_Out.Columns.Add(CMain._T(TX.AREA), 100, HorizontalAlignment.Left)
        LW_Out.Columns.Add(CMain._T(TX.NAME), 150, HorizontalAlignment.Left)
        
        Me.txIn.Text = CM_PolyArea.Stgs.sTxLstA

        Me.txIn.Focus()
        Me.txIn.SelectAll()
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    Private Sub TabMain_SelectedIndexChanged( _
        ByVal sender As System.Object,  _
        ByVal e As System.EventArgs) _
        Handles TabMain.SelectedIndexChanged

        If (TabMain.SelectedIndex = 1) Then
            ParseUIText()
        End If

    End Sub
    '// ===================================================================== '

    #End Region
    ' /////////////////////////////////////////////////////////////////////////



End Class
'//  CLASS    =============================================================== '
'//
'//
'//
'//
'//  FUNCTIONS    ----------------------------------------------------------- '

'//  FUNCTIONS    =========================================================== '
'//
'//
'//
'//
'// ------------------------------------------------------------------------- '
'// ========================================================================= '
